package com.example.gustavo.hello.utils;

import android.os.Handler;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by gustavo on 2014/12/30.
 */
public class DateUtil {

    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("MM月dd日 HH:mm");
    private static SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm");

    /**
     * 格式化时间, eg:今天 14:52, 昨天 14:25, 12月28日 14:41, 2013年12月30日 14:42
     * @param milliseconds  时间戳
     * @return
     */
    public static String formatDate(long milliseconds){
        Date date = new Date(milliseconds);
        Calendar calendar = Calendar.getInstance();

        int year_today = calendar.get(Calendar.YEAR);
        int month_today = calendar.get(Calendar.MONTH);
        int day_today = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.setTime(date);
        int year_date = calendar.get(Calendar.YEAR);
        int month_date = calendar.get(Calendar.MONTH);
        int day_date = calendar.get(Calendar.DAY_OF_MONTH);

        String format = "";
        if(year_date == year_today){
            format = sdf2.format(date);

            if(month_date == month_today){
                if(day_date  == day_today){
                    format = "今天 " + sdf3.format(date);
                }else if(day_date + 1 == day_today){
                    format = "昨天 " + sdf3.format(date);
                }
            }
        }else{
            format = sdf1.format(date);
        }

        return format;
    }
}
